package com.thd.springboottest.camunda.controller;

import com.thd.springboottest.camunda.service.WorkFlowService;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * com.thd.springboottest.camunda.controller.CamundaController
 *
 * @author: wanglei62
 * @DATE: 2022/5/26 17:01
 **/
@Controller
@RequestMapping("/camunda/parallelTask")
public class Camunda05parallelTask {
    @Autowired
    private ProcessEngine processEngine;
    @Autowired
    private WorkFlowService workFlowService;
    /**
     * 部署
     */
    // url : http://127.0.0.1:8899/thd/camunda/parallelTask/deploy
    @RequestMapping(value="/deploy",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<String> deploy(){
        String deployId = this.workFlowService.deploy("processes/05parallelTask.bpmn");
        System.out.println("deployId:" + deployId);
        return ResponseEntity.ok("SUCCESS");
    }

    /**
     * 开启流程
     */
    // url : http://127.0.0.1:8899/thd/camunda/parallelTask/startProcess/[businessKey:gateway001]
    @RequestMapping(value="/startProcess/{businessKey}",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<String> startProcess(@PathVariable String businessKey){
        ProcessInstance pi = processEngine.getRuntimeService().startProcessInstanceByKey("parallelTask",businessKey);
       System.out.println("流程实例ID:" + pi.getProcessInstanceId());
        return ResponseEntity.ok("SUCCESS");
    }

    /*
    设置流程变量
    http://127.0.0.1:8899/thd/camunda/addVariableForProcessInstance/[processInstanceId]

    {
    "users":["zhangsan","lisi","wangwu","zhaoliu"]
    }
     */

    // 以下开始测试并行task

    /*
    完成代办http://127.0.0.1:8899/thd/camunda/completeTask/f462fb36-dffd-11ec-908d-e86a64fc0114
     */

    /*
    查询待办  http://127.0.0.1:8899/thd/camunda/queryTaskByBusinessKey/a01
    待办为4个TaskB , 待办人是流程变量users中的 zhangsan,lisi,wangwu,zhaoliu
     */

    /*
    设置流程变量 loop =1
    http://127.0.0.1:8899/thd/camunda/addVariableForProcessInstance/9e79ebf1-e092-11ec-8fae-e86a64fc0114
    {
        "loop":1
    }
     */

    /*
    完成zhangsan代办  http://127.0.0.1:8899/thd/camunda/completeTask/[taskId]
     */

    /*
    查询待办  http://127.0.0.1:8899/thd/camunda/queryTaskByBusinessKey/a01
    待办为3个TASKB，待办人分为为lisi,wangwu,zhaoliu
     */


    /*
    设置流程变量
    http://127.0.0.1:8899/thd/camunda/addVariableForProcessInstance/[processInstanceId]
    {
        "loop":2
    }
     */
     /*
    完成lisi代办http://127.0.0.1:8899/thd/camunda/completeTask/[taskId]
     */

     /*
    查询待办  http://127.0.0.1:8899/thd/camunda/queryTaskByBusinessKey/a01
    待办为2个TASKB，待办人分为为wangwu,zhaoliu
     */

     /*
    设置流程变量
    http://127.0.0.1:8899/thd/camunda/addVariableForProcessInstance/[processInstanceId]
    {
        "loop":3
    }
     */
      /*
    完成代办http://127.0.0.1:8899/thd/camunda/completeTask/[taskId]
     */


       /*
    查询待办  http://127.0.0.1:8899/thd/camunda/queryTaskByBusinessKey/a01
    看到待办已经跳出了TaskB,进入到TASKC中
    因为流程变量loop>2了(参见bpmn中TASKB节点的 Completion condition) ， 所以跳出了并行任务TaskB
     */


}
